package indigo.captcha.image;

import java.security.SecureRandom;
import java.util.Random;

import com.jhlabs.image.RippleFilter;
import com.jhlabs.image.TransformFilter;

public class RandomRippleDeformer extends ThreadLocalFilterDeformer<RippleFilter> {
	private final Random random = new SecureRandom();
	private final int width;
	private final int height;
	private final int maxFontSize;

	public RandomRippleDeformer(int width, int height, int maxFontSize) {
		this.width = width;
		this.height = height;
		this.maxFontSize = maxFontSize;
	}

	@Override
	protected RippleFilter createFilter() {
		RippleFilter filter = new RippleFilter();
		filter.setEdgeAction(TransformFilter.CLAMP);
		filter.setWaveType(RippleFilter.SINE);
		filter.setXAmplitude(0f);
		filter.setXWavelength(height);
		return filter;
	}

	@Override
	protected void prepareFilter(RippleFilter filter) {
		filter.setYAmplitude((height - maxFontSize) / 2 * random.nextFloat());
		filter.setYWavelength(width - width / 2 * random.nextFloat());
	}
}
